import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 了解迭代器
 */
public class CollectionIterator {
    public static void main(String[] args) {
        Collection<String> list = new ArrayList<>();
        list.add("刘能");
        list.add("赵四");
        list.add("谢大脚");
        list.add("王长贵");
        System.out.println(list);

        //使用next方法取当前迭代器的值并自增
        Iterator<String> it = list.iterator();
//        System.out.println(it.next());
//        System.out.println(it.next());
//        System.out.println(it.next());
//        System.out.println(it.next());

        //HashNext就是会判断当前位置是否有元素,若有才执行
        while (it.hasNext()){
            System.out.println(it.next());
        }

        //同样支持增强for
        for(String each:list){
            System.out.println(each);
        }

        //运用foreach配合lambda简化遍历
        list.forEach(s -> System.out.println(s));
    }
}
